package com.lw;

import com.lw.demo.VelocityDemo1;
import com.lw.demo.VelocityDemo2;
import com.lw.domain.Person;
import com.lw.helloWordDemo.VelocityDemo;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.junit.Before;
import org.junit.Test;
import org.thymeleaf.util.DateUtils;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import java.util.Properties;

public class VelocityDemoTest {

	@Test
	public void initVecocity() {
		VelocityDemo demo = new VelocityDemo();
		demo.initVecocity();
	}

	@Test
	public void test() {
		VelocityDemo1 demo1 = new VelocityDemo1();
		demo1.initVecocity();
	}

	@Test
	public void test1() {
		VelocityDemo2 demo2 = new VelocityDemo2();
		demo2.initVecocity();
	}
	/**
	 * 对象赋值以及访问
	 */
	@Test
	public void test2() throws IOException {

		//1 加载配置
		Properties properties = new Properties();
		properties.load(Test.class.getClassLoader().getResourceAsStream("velocity.properties"));
		Velocity.init(properties);

		//2 创建上下文对象
		VelocityContext context = new VelocityContext();
		//3 赋值
		Person person = new Person();
		person.setAge(23);
		person.setId(1);
		person.setName("隔壁老爿");
		context.put("person", person);
		//4选择模板
		Template template = Velocity.getTemplate("/template/person.vm");
		//5 合并模板和数据并输出
		StringWriter sw = new StringWriter();
		template.merge(context, sw);
		sw.flush();
		System.out.println("==========================================");
		System.out.println(sw.toString());
	}

 
}